home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / CompoundEdit.java < prev    next >
Text File  |  1998-06-30  |  6KB  |  232 lines

  1. /*
  2.  * @(#)CompoundEdit.java    1.8 97/08/15
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing.undo;
  21.  
  22. import java.util.*;
  23.  
  24. /**
  25.  * A concrete subclass of AbstractUndoableEdit, used to assemble little
  26.  * UndoableEdits into great big ones.
  27.  */
  28. public class CompoundEdit extends AbstractUndoableEdit {
  29.     /**
  30.      * True iff this edit has never received end()
  31.      */
  32.     boolean inProgress;
  33.  
  34.     /**
  35.      * The collection of UndoableEdits undone/redone en masse by this
  36.      * CompoundEdit
  37.      */ 
  38.     protected Vector edits;
  39.  
  40.     public CompoundEdit() {
  41.     super();
  42.     inProgress = true;
  43.     edits = new Vector();
  44.     }
  45.  
  46.     /**
  47.      * Sends undo() to all contained UndoableEdits in the reverse of
  48.      * the order in which they were added.
  49.      */
  50.     public void undo() throws CannotUndoException {
  51.     super.undo();
  52.     int i = edits.size();
  53.     while (i-- > 0) {
  54.         UndoableEdit e = (UndoableEdit)edits.elementAt(i);
  55.         e.undo();
  56.     }
  57.     }
  58.  
  59.     /**
  60.      * Sends redo() to all contained UndoableEdits in the order in
  61.      * which they were added.
  62.      */
  63.     public void redo() throws CannotRedoException {
  64.     super.redo();
  65.     Enumeration cursor = edits.elements();
  66.     while (cursor.hasMoreElements()) {
  67.         ((UndoableEdit)cursor.nextElement()).redo();
  68.     }
  69.     }
  70.  
  71.     /**
  72.      * Returns the last UndoableEdit in edits, or null if edits is
  73.      * empty
  74.      */
  75.     protected UndoableEdit lastEdit() {
  76.     int count = edits.size();
  77.     if (count > 0)
  78.         return (UndoableEdit)edits.elementAt(count-1);
  79.     else
  80.         return null;
  81.     }
  82.  
  83.     /**
  84.      * Send die to each subedit, in the reverse of the order that they
  85.      * were added
  86.      */
  87.     public void die() {
  88.     int size = edits.size();
  89.     for (int i = size-1; i >= 0; i--)
  90.     {
  91.         UndoableEdit e = (UndoableEdit)edits.elementAt(i);
  92. //         System.out.println("CompoundEdit(" + i + "): Discarding " +
  93. //                    e.getUndoPresentationName());
  94.         e.die();
  95.     }
  96.     super.die();
  97.     }    
  98.  
  99.     /**
  100.      * If this edit is inProgress, accepts anEdit and returns
  101.      * true.
  102.      * 
  103.      *  <p>The last edit added to this CompoundEdit is given a
  104.      * chance to addEdit(anEdit). If it refuses (returns false), anEdit is
  105.      * given a chance to replaceEdit the last edit. If anEdit returns
  106.      * false here, it is added to edits.</p>
  107.      */ 
  108.     public boolean addEdit(UndoableEdit anEdit) {
  109.     if (!inProgress) {
  110.         return false;
  111.     } else {
  112.         UndoableEdit last = lastEdit();
  113.  
  114.         // If this is the first subedit received, just add it.
  115.         // Otherwise, give the last one a chance to absorb the new
  116.         // one.  If it won't, give the new one a chance to absorb
  117.         // the last one.
  118.  
  119.         if (last == null) {
  120.         edits.addElement(anEdit);
  121.         }
  122.         else if (!last.addEdit(anEdit)) {
  123.         if (anEdit.replaceEdit(last)) {
  124.             edits.removeElementAt(edits.size()-1);
  125.         }
  126.         edits.addElement(anEdit);
  127.         }
  128.  
  129.         return true;
  130.     }
  131.     }
  132.  
  133.     /**
  134.      * Sets inProgress to false.
  135.      * 
  136.      * @see #canUndo
  137.      * @see #canRedo
  138.      */
  139.     public void end() {
  140.     inProgress = false;
  141.     }
  142.  
  143.     /**
  144.      * Returns false if isInProgress or if super does.
  145.      * 
  146.      * @see    #isInProgress
  147.      */
  148.     public boolean canUndo() {
  149.     return !isInProgress() && super.canUndo();
  150.     }
  151.  
  152.     /**
  153.      * Returns false if isInProgress or if super does.
  154.      * 
  155.      * @see    #isInProgress
  156.      */
  157.     public boolean canRedo() {
  158.     return !isInProgress() && super.canRedo();
  159.     }
  160.  
  161.     /**
  162.      * Returns true if this edit is in progress--that is, it has not
  163.      * received end. This generally means that edits are still being
  164.      * added to it.
  165.      *
  166.      * @see    #end
  167.      */
  168.     public boolean isInProgress() {
  169.     return inProgress;
  170.     }
  171.  
  172.     /**
  173.      * Returns true if any of the UndoableEdits in edits do. Returns
  174.      * false if they all return false.
  175.      */
  176.     public boolean  isSignificant() {
  177.     Enumeration cursor = edits.elements();
  178.     while (cursor.hasMoreElements()) {
  179.         if (((UndoableEdit)cursor.nextElement()).isSignificant()) {
  180.         return true;
  181.         }
  182.     }
  183.     return false;
  184.     }
  185.  
  186.     /**
  187.      * Returns getPresentationName from the last UndoableEdit added to
  188.      * edits. If edits is empty, calls super.
  189.      */
  190.     public String getPresentationName() {
  191.     UndoableEdit last = lastEdit();
  192.     if (last != null) {
  193.         return last.getPresentationName();
  194.     } else {
  195.         return super.getPresentationName();
  196.     }
  197.     }
  198.         
  199.     /**
  200.      * Returns getUndoPresentationName from the last UndoableEdit
  201.      * added to edits. If edits is empty, calls super.
  202.      */
  203.     public String getUndoPresentationName() {
  204.     UndoableEdit last = lastEdit();
  205.     if (last != null) {
  206.         return last.getUndoPresentationName();
  207.     } else {
  208.         return super.getUndoPresentationName();
  209.     }
  210.     }
  211.         
  212.     /**
  213.      * Returns getRedoPresentationName from the last UndoableEdit
  214.      * added to edits. If edits is empty, calls super.
  215.      */
  216.     public String getRedoPresentationName() {
  217.     UndoableEdit last = lastEdit();
  218.     if (last != null) {
  219.         return last.getRedoPresentationName();
  220.     } else {
  221.         return super.getRedoPresentationName();
  222.     }
  223.     }
  224.         
  225.     public String toString()
  226.     {
  227.     return super.toString()
  228.         + " inProgress: " + inProgress
  229.         + " edits: " + edits;
  230.     }
  231. }
  232.